﻿#include "MyTreeWidget.h"

#include "mytreewidget.h"
#include "mymainwindow.h"
#include <QContextMenuEvent>
#include <QMessageBox>
#include <QCursor>
#include <QItemSelectionModel>
#include <QRect>
#include <QList>
#include <QSize>
#include <QPalette>
#include <QColor>
#include <QTreeWidgetItem>
#include "SaveDevToModelFile.h"

MyTreeWidget::MyTreeWidget(QWidget *parent) : QTreeWidget(parent)
{
//	InitTableTreeItem();

	m_LastSelectItem = NULL;
	setSortingEnabled(false);
	setDragEnabled(false);
	setAutoScroll(true);
	setColumnCount(1);
	setHeaderHidden(true);
	setRootIsDecorated(true);
	setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
	setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
	//setupMenuBar();
	setSelectionBehavior(QAbstractItemView::SelectRows);
	setSelectionMode(QAbstractItemView::SingleSelection);
	setAutoFillBackground(true);

	QColor tempcolor;
	QPalette  oldPalette = this->palette();
	tempcolor = oldPalette.color(QPalette::Highlight);
	oldPalette.setColor(QPalette::Highlight, tempcolor);
	oldPalette.setColor(QPalette::HighlightedText, Qt::white);
	setPalette(oldPalette);
	connect(this, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, SLOT(OnClickItem(QTreeWidgetItem *)));
}

MyTreeWidget::~MyTreeWidget()
{

}

//初始化表结构树Item
int MyTreeWidget::InitTableTreeItem()
{
	if (m_LastSelectItem)
	{
		m_LastSelectItem = NULL;
	}

	clear();
	QString			ItemText;
	TreeItemData	ItemUserData;
	int				ItemFlag = 0;
	QVariant		ItemVariData;

	QTreeWidgetItem *m_pCommItem = NULL;
	QTreeWidgetItem *m_pNSA3000TItem = NULL;
	ItemFlag = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
	/*m_pCommItem = new QTreeWidgetItem(this);
	ItemText = QString::fromLocal8Bit("公用表");
	ItemUserData.AppType = SYSTEM_TYPE_NSA_CD;
	ItemVariData.setValue<TreeItemData>(ItemUserData);
	if (m_pCommItem)
	{
		SetItem(m_pCommItem, ItemText, ItemVariData, ItemFlag);
		InitCommTableItem(m_pCommItem);
		setItemExpanded(m_pCommItem, true);
	}*/

	m_pNSA3000TItem = new QTreeWidgetItem(this);
	ItemText = QString::fromLocal8Bit("远动表");
	ItemUserData.AppType = SYSTEM_TYPE_NSA_YD;
	if (m_pNSA3000TItem)
	{
		ItemVariData.setValue<TreeItemData>(ItemUserData);
		SetItem(m_pNSA3000TItem, ItemText, ItemVariData, ItemFlag);  
		InitYDTypeItem(SYSTEM_TYPE_NSA_YD,m_pNSA3000TItem);
		setItemExpanded(m_pNSA3000TItem, true);
	}
	return 0;
}

int MyTreeWidget::InitCommTableItem(QTreeWidgetItem *pParentItem)
{
	if (!pParentItem)
	{
		return -1;
	}

	char		*pBuff = NULL;
	int		Buff_len = 0;
	int		TableNum = 0;
	int		i = 0;

	TableNum = g_DbCfgFunObj.GetTableInfobyAppType(SYSTEM_TYPE_NSA_CD, pBuff, &Buff_len);
	if (TableNum > 0 && pBuff && Buff_len >0)
	{
		for (i=0; i<TableNum; i++)
		{
			AddNewItem(SYSTEM_TYPE_NSA_CD, TABLE_CTABLE_INFO, 0, (char*)((cTable_Info*)pBuff+i), pParentItem, 0, 0);
		}
	}

	if (pBuff)
	{
		delete [](char*)pBuff;
		pBuff = NULL;
	}

	return 0;
}


//初始化远动库使用分类Item
int MyTreeWidget::InitYDTypeItem(int AppType, QTreeWidgetItem *pParentItem)
{
	if (AppType != SYSTEM_TYPE_NSA_YD || !pParentItem)
	{
		return -1;
	}

	QTreeWidgetItem *pNewItem = NULL;

	int		UseType = 0;

	QString			ItemText;
	TreeItemData	ItemUserData;	
	QVariant		ItemVariData;

	for (UseType = 1; UseType<NSA3100E_USER_TYPE_MAX_NUM; UseType++)
	{
		ItemUserData.itemtype = 0;
		ItemUserData.AppType = AppType;
		ItemUserData.parentId = 0;
		ItemUserData.RecordId = 0;
		ItemUserData.ShowTableId = 0;
		ItemUserData.ParentTableId = 0;
		switch (UseType)
		{
		case NSA3100E_USER_TYPE_TASK:
			{
				pNewItem = new QTreeWidgetItem(pParentItem);
				ItemText = QString::fromLocal8Bit("任务表");	
				
				ItemUserData.AppType=SYSTEM_TYPE_NSA_YD;
				ItemUserData.ShowTableId=TABLE_RTASK_INFO;
				ItemVariData.setValue<TreeItemData>(ItemUserData);
				SetItem(pNewItem, ItemText, ItemVariData);
				if (pNewItem)
				{
					InitRecordItem(SYSTEM_TYPE_NSA_YD, TABLE_RTASK_INFO, 0, pNewItem);
				}
			}
			break;
		case NSA3100E_USER_TYPE_TRANSMIT:
			{	
				pNewItem = new QTreeWidgetItem(pParentItem);
				ItemText = QString::fromLocal8Bit("转发配置");
				ItemUserData.AppType=SYSTEM_TYPE_NSA_YD;
				ItemUserData.ShowTableId=TABLE_RTRANSMITCONFIG_INFO;	
				ItemVariData.setValue<TreeItemData>(ItemUserData);
				SetItem(pNewItem, ItemText, ItemVariData);
				if (pNewItem)
				{
					InitRecordItem(SYSTEM_TYPE_NSA_YD, TABLE_RTRANSMITCONFIG_INFO, 0, pNewItem);
				}
			}
			break;
		case NSA3100E_USER_TYPE_CAL:
			{	
				pNewItem = new QTreeWidgetItem(pParentItem);
				ItemText = QString::fromLocal8Bit("二次计算");	
				ItemUserData.AppType=SYSTEM_TYPE_NSA_YD;
				ItemUserData.ShowTableId=TABLE_REXPDEFINE_INFO;
				ItemVariData.setValue<TreeItemData>(ItemUserData);
				SetItem(pNewItem, ItemText, ItemVariData);
				if (pNewItem)
				{
					//UpdateEquipChildItem(pNewItem);
					InitRecordItem(SYSTEM_TYPE_NSA_YD, TABLE_REXPDEFINE_INFO, 0, pNewItem);
				}
			}
			break;
		case NSA3100E_USER_TYPE_SYS_PARAM:
			{
				pNewItem = new QTreeWidgetItem(pParentItem);
				ItemText = QString::fromLocal8Bit("终端参数");	
				ItemUserData.itemtype = NSA3100E_USER_TYPE_SYS_PARAM;
				ItemUserData.AppType=SYSTEM_TYPE_NSA_YD;
				ItemUserData.ShowTableId=0;
				ItemVariData.setValue<TreeItemData>(ItemUserData);
				SetItem(pNewItem, ItemText, ItemVariData);
				if (pNewItem)
				{
					UpdateSysChildItem(pNewItem);
					UpdateTaskChildItem(0, false,false,pNewItem);
				//	InitRecordItem(SYSTEM_TYPE_NSA_YD, TABLE_RSETPARA_INDEX, 0, pNewItem);
				}
			}
			break;
		//17.4.21添加总表显示项目
		case NSA3100E_USER_TYPE_TOTAL_SHOW:
			{
				pNewItem = new QTreeWidgetItem(pParentItem);
				ItemText = QString::fromLocal8Bit("总表显示");	
				ItemUserData.itemtype = NSA3100E_USER_TYPE_TOTAL_SHOW;
				ItemUserData.AppType=SYSTEM_TYPE_NSA_YD;
				ItemUserData.ShowTableId=0;
				ItemVariData.setValue<TreeItemData>(ItemUserData);
				SetItem(pNewItem, ItemText, ItemVariData);
				if (pNewItem)
				{
					UpdateDeviceChildItem(0, pNewItem);
				//	UpdateSysChildItem(pNewItem);
				//	UpdateTaskChildItem(0, false,false,pNewItem);
					//	InitRecordItem(SYSTEM_TYPE_NSA_YD, TABLE_RSETPARA_INDEX, 0, pNewItem);
				}
			}
			break;
		//ccg17.5.5添加保护逻辑节点显示
		case NSA3100E_USER_TYPE_61850_MODEL:
			{
				pNewItem = new QTreeWidgetItem(pParentItem);
				ItemText = QString::fromLocal8Bit("61850模型配置");	
				ItemUserData.itemtype = NSA3100E_USER_TYPE_61850_MODEL;
				ItemUserData.AppType=SYSTEM_TYPE_NSA_YD;
				ItemUserData.ShowTableId=TABLE_RLOGIC_DEVICE;
				ItemVariData.setValue<TreeItemData>(ItemUserData);
				SetItem(pNewItem, ItemText, ItemVariData);
				if (pNewItem)
				{
					
					InitRecordItem(SYSTEM_TYPE_NSA_YD,TABLE_RLOGIC_DEVICE , 0, pNewItem);
				}
			}
			break;



		case NSA3100E_USER_TYPE_MODEL:
			{
				pNewItem = new QTreeWidgetItem(pParentItem);
				ItemText = QString::fromLocal8Bit("设备模板");
				ItemUserData.ShowTableId = TABLE_RDEVMODEL_INFO;	
				ItemVariData.setValue<TreeItemData>(ItemUserData);
				SetItem(pNewItem, ItemText, ItemVariData);
				if (pNewItem)
				{
					InitRecordItem(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_INFO, 0, pNewItem);
				}
			}
			break;
		default:
			break;
		}
	}
	return 0;
}

// 初始化实际记录Item
int MyTreeWidget::InitRecordItem(int AppType, int TableId, int ParentId, QTreeWidgetItem *pParentItem, int NewRecordId)
{
	if (!pParentItem || AppType <=0 || TableId <= 0)
	{
		return -1;
	}

	QTreeWidgetItem *pNewItem = NULL;
	char		*pBuff = NULL;
	int		Buff_len = 0;
	int		RecordNum = 0;
	int		i = 0;
	if (SYSTEM_TYPE_NSA_YD == AppType)
	{
		switch (TableId)
		{
		case TABLE_RTASK_INFO:				//任务表
			{
				RecordNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId(AppType, TableId, pBuff, &Buff_len, true);
				if (RecordNum>0 && pBuff && Buff_len>0)
				{
					for (i=0; i<RecordNum; i++)
					{
						pNewItem = AddNewItem(AppType, TableId, ((rTask_Info*)pBuff)[i].ID+PARENT_OFFSET*TABLE_RTASK_INFO, (char*)((rTask_Info*)pBuff+i), pParentItem, 0, 0);
						if (pNewItem)
						{
							UpdateTaskChildItem(((rTask_Info*)pBuff)[i].ID,TRUE,FALSE,pNewItem);
						}
					}
				}

				if (pBuff)
				{	
					delete []pBuff;
					pBuff = NULL;
				}

			}
			break;
		case TABLE_RDEVS_INFO:				//设备表
			{
				RecordNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(AppType, TableId, ParentId, pBuff, &Buff_len,true);
				if (RecordNum>0 && pBuff && Buff_len>0)
				{
					for (i=0; i<RecordNum; i++)
					{
						if ( (NewRecordId > 0 && NewRecordId == ((rDevS_Info*)pBuff)[i].ID) || NewRecordId == 0)
						{
							pNewItem = AddNewItem(AppType, TableId, ((rDevS_Info*)pBuff)[i].ID+PARENT_OFFSET*TABLE_RDEVS_INFO, (char*)((rDevS_Info*)pBuff+i), pParentItem, 0, 0);
							if (pNewItem)
							{
								//InitRecordItem(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_INFO, ((rDevS_Info*)pBuff)[i].ID+PARENT_OFFSET*TABLE_RDEVS_INFO, pNewItem);
								UpdateDeviceChildItem(((rDevS_Info*)pBuff)[i].ID, pNewItem);
							}
						}
					}
				}

				if (pBuff)
				{	
					delete []pBuff;
					pBuff = NULL;
				}
			}
			break;
		case TABLE_RLOGIC_DEVICE:				//61850模型设备
			{
				RecordNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId(AppType, TableId, pBuff, &Buff_len, true);
				if (RecordNum>0 && pBuff && Buff_len>0) 
				{
					for (i=0; i<RecordNum; i++)
					{
						pNewItem = AddNewItem(AppType, TableId, ((rLogic_Device*)pBuff)[i].ID+PARENT_OFFSET*TABLE_RLOGIC_DEVICE, (char*)((rLogic_Device*)pBuff+i), pParentItem, 0, 0);
						if (pNewItem)
						{
							InitRecordItem(SYSTEM_TYPE_NSA_YD, TABLE_RLOGIC_NODE, ((rLogic_Device*)pBuff)[i].ID+TABLE_RLOGIC_DEVICE*PARENT_OFFSET, pNewItem);
						}
					}
				}

				if (pBuff)
				{	
					delete []pBuff;
					pBuff = NULL;
				}
			}
			break;
		case TABLE_RLOGIC_NODE:							//61850模型逻辑节点	
			{
				RecordNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(AppType, TableId, ParentId/*+PARENT_OFFSET*TABLE_RLOGIC_DEVICE*/, pBuff, &Buff_len, true);
				if (RecordNum>0 && pBuff && Buff_len>0) 
				{
					for (i=0; i<RecordNum; i++)
					{
						pNewItem = AddNewItem(AppType, TableId, ((rLogic_Node*)pBuff)[i].ID+PARENT_OFFSET*TABLE_RLOGIC_NODE, (char*)((rLogic_Node*)pBuff+i), pParentItem, 0, 0);
						
					}
				}

				if (pBuff)
				{	
					delete []pBuff;
					pBuff = NULL;
				}
			}
			break;
		 
		case TABLE_RTRANSMITCONFIG_INFO:				//转发配置
			{
				RecordNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId(AppType, TableId, pBuff, &Buff_len,true);
				if (RecordNum>0 && pBuff && Buff_len>0)
				{
					for (i=0; i<RecordNum; i++)
					{
						if ( (NewRecordId > 0 && NewRecordId == ((rTransmitConfig_Info*)pBuff)[i].ID) || NewRecordId == 0)
						{
							pNewItem = AddNewItem(AppType, TableId, ((rTransmitConfig_Info*)pBuff)[i].ID+PARENT_OFFSET*TABLE_RTRANSMITCONFIG_INFO, (char*)((rTransmitConfig_Info*)pBuff+i), pParentItem, 0, 0);
							if (pNewItem)
							{
								//InitRecordItem(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_INFO, ((rDevS_Info*)pBuff)[i].ID+PARENT_OFFSET*TABLE_RDEVS_INFO, pNewItem);
								UpdateTransmitChildItem(((rTransmitConfig_Info*)pBuff)[i].ID+PARENT_OFFSET*TABLE_RTRANSMITCONFIG_INFO, pNewItem);
							}
						}
					}
				}

				if (pBuff)
				{	
					delete []pBuff;
					pBuff = NULL;
				}
			}
			break;

		case TABLE_RDEVMODEL_INFO:				//设备模板表
			{
				RecordNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId(AppType, TableId, pBuff, &Buff_len, true);
				if (RecordNum>0 && pBuff && Buff_len>0)
				{
					for (i=0; i<RecordNum; i++)
					{
						if ( (NewRecordId > 0 && NewRecordId == ((rDevModel_Info*)pBuff)[i].ID) || NewRecordId == 0)
						{
							pNewItem = AddNewItem(AppType, TableId, 0, (char*)((rDevModel_Info*)pBuff+i), pParentItem, 0, 0);
							if (pNewItem)
							{
								UpdateDeviceModelChildItem(((rDevModel_Info*)pBuff)[i].ID,pNewItem);
							}
						}
					}
				}

				if (pBuff)
				{	
					delete []pBuff;
					pBuff = NULL;
				}
			}
			break;
		case TABLE_REXPDEFINE_INFO:
			{
				RecordNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId(AppType, TableId, pBuff, &Buff_len, true);
				if (RecordNum>0 && pBuff && Buff_len>0)
				{
					for (i=0; i<RecordNum; i++)
					{
						if ( (NewRecordId > 0 && NewRecordId == ((rExpDefine_Info*)pBuff)[i].ID) || NewRecordId == 0)
						{
							pNewItem = AddNewItem(AppType, TableId, 0, (char*)((rExpDefine_Info*)pBuff+i), pParentItem, 0, 0);
						}
					}
				}

				if (pBuff)
				{	
					delete []pBuff;
					pBuff = NULL;
				}
			}
			break;

// 		case TABLE_RSETPARA_INDEX:
// 			RecordNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId(AppType, TableId, pBuff, &Buff_len, true);
// 			if (RecordNum > 0 && pBuff && Buff_len >0)
// 			{
// 				for (i=0; i<RecordNum; i++)
// 				{
// 					if ( (NewRecordId > 0 && NewRecordId == ((rSetPara_Index*)pBuff)[i].ID) || NewRecordId == 0)
// 					{
// 						pNewItem = AddNewItem(AppType, TableId, 0, (char*)((rSetPara_Index*)pBuff+i), pParentItem, 0, 0);
// 						if (pNewItem)
// 						{
// 						//	UpdateDeviceModelChildItem(((rDevModel_Info*)pBuff)[i].ID,pNewItem);
// 						}
// 					}
// 				}
// 
// 			//	AddNewItem(SYSTEM_TYPE_NSA_YD, TABLE_RSETPARA_INFO, 0, (char*)((cTable_Info*)pBuff), pParentItem, 0, 0);
// 			}
// 			break;
 		default:
 			break;
 		}

	}

	if (pBuff)
	{	
		delete []pBuff;
		pBuff = NULL;
	}

	return 0;
}

// 修改单个任务的子Item
int MyTreeWidget::UpdateTaskChildItem(int TaskId, bool IsCollect, bool IsTransmit, QTreeWidgetItem *pFRoutItem)
{
	if (!pFRoutItem)
	{
		return 0;
	}
	QTreeWidgetItem *pNewItem = NULL;
	QString			 ItemText;
	TreeItemData	ItemUserData;
	QVariant		ItemVariData;

	if (TaskId>0)
	{
		pNewItem = new QTreeWidgetItem(pFRoutItem);
		ItemText = QString::fromLocal8Bit("设备");

		ItemUserData.AppType = SYSTEM_TYPE_NSA_YD;
		ItemUserData.parentId = TaskId+TABLE_RTASK_INFO*PARENT_OFFSET;
		ItemUserData.ShowTableId = TABLE_RDEVS_INFO;
		ItemUserData.ParentTableId = TABLE_RTASK_INFO;
		ItemVariData.setValue<TreeItemData>(ItemUserData);
		SetItem(pNewItem, ItemText,ItemVariData);
		if (pNewItem)
		{
			InitRecordItem(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_INFO, ItemUserData.parentId, pNewItem);
		}
	}else
	{
		pNewItem = new QTreeWidgetItem(pFRoutItem);
		ItemText = QString::fromLocal8Bit("终端设备");

		ItemUserData.AppType = SYSTEM_TYPE_NSA_YD;
		ItemUserData.parentId = TABLE_RPARAMS_TERMINAL*PARENT_OFFSET;
		ItemUserData.ShowTableId = TABLE_RDEVS_INFO;
		ItemUserData.ParentTableId = TABLE_RPARAMS_TERMINAL;
		ItemVariData.setValue<TreeItemData>(ItemUserData);
		SetItem(pNewItem, ItemText,ItemVariData);
		if (pNewItem)
		{
			InitRecordItem(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_INFO, ItemUserData.parentId, pNewItem);
		}
	}
	

	return 1;
}

//修改单个61850模型逻辑设备下面的子Item
/*
int MyTreeWidget::Update61850ModelChildItem(int DeviceID, QTreeWidgetItem *pFRoutItem)
{
	if (!pFRoutItem)
	{
		return 0;
	}
	/ *QTreeWidgetItem *pNewItem = NULL;
	QString			 ItemText;
	TreeItemData	ItemUserData;
	QVariant		ItemVariData;


	pNewItem = new QTreeWidgetItem(pFRoutItem);
	ItemText = pFRoutItem->text(0);

	ItemUserData.AppType = SYSTEM_TYPE_NSA_YD;
	ItemUserData.parentId = DeviceID+TABLE_RLOGIC_DEVICE*PARENT_OFFSET;
	ItemUserData.ShowTableId = TABLE_RDATA_OBJECT;
	ItemUserData.ParentTableId = TABLE_RLOGIC_DEVICE;
	ItemVariData.setValue<TreeItemData>(ItemUserData);
	SetItem(pNewItem, ItemText,ItemVariData);* /
	/ *if (pNewItem)
	{
		* /
	//InitRecordItem(int AppType, int TableId, int ParentId, QTreeWidgetItem *pParentItem, int NewRecordId)
	
	InitRecordItem(SYSTEM_TYPE_NSA_YD, TABLE_RLOGIC_NODE, DeviceID, pFRoutItem);
		
	/ *}* /



	return 1;
}
*/

// 修改转发的子Item
int MyTreeWidget::UpdateTransmitChildItem(int ParentId, QTreeWidgetItem *pParentItem)
{
	if (ParentId > 0)
	{
		QTreeWidgetItem *pNewItem = NULL;
		QString			 ItemText;
		TreeItemData	 ItemUserData;
		QVariant		 ItemVariData;
		int				 TableNo = 0;
		bool			 bAddNew = false;
		for (TableNo = TABLE_RTRANSMIT_YX; TableNo <= TABLE_RTRANSMIT_YK; TableNo++)
		{
			ItemUserData.AppType = SYSTEM_TYPE_NSA_YD;
			ItemUserData.parentId = ParentId;
			ItemUserData.ParentTableId = TABLE_RTASK_INFO;
			ItemUserData.ShowTableId = TableNo;
			bAddNew = true;

			switch (TableNo)
			{
			case TABLE_RTRANSMIT_YX:
				{
					ItemText = QString::fromLocal8Bit("遥信转发表");
				}
				break;
			case TABLE_RTRANSMIT_YC:
				{
					ItemText = QString::fromLocal8Bit("遥测转发表");
				}
				break;
			case TABLE_RTRANSMIT_YK:
				{
					ItemText = QString::fromLocal8Bit("遥控转发表");
				}
				break;
			case TABLE_RTRANSMIT_YM:
				{
					ItemText = QString::fromLocal8Bit("遥脉转发表");
				}
				break;
			default:
				{
					bAddNew = false;
				}
				break;
			}

			if (bAddNew)
			{
				pNewItem = new QTreeWidgetItem(pParentItem);
				ItemVariData.setValue<TreeItemData>(ItemUserData);
				SetItem(pNewItem, ItemText,ItemVariData);
			}
		}
		return 1;
	}

	return 0;
}


//ccg17.05.03更新总表显示遥信分类子项目
int MyTreeWidget::UpdateTotalShowChildItem(int ParentId, QTreeWidgetItem *pParentItem)
{

	QTreeWidgetItem *pNewItem = NULL;
	QString			 ItemText;
	TreeItemData	 ItemUserData;
	QVariant		 ItemVariData;
	int				 TableNo = 0;
	bool			 bAddNew = false;
	for (int i= TABLE_TOTALSHOW_DZYJ ; i < TABLE_TOTALSHOW_MAXNUM; i++)
	{
		ItemUserData.AppType = SYSTEM_TYPE_NSA_YD;
		ItemUserData.parentId = 0;
		ItemUserData.ParentTableId = TABLE_RTASK_INFO;
		ItemUserData.ShowTableId = TABLE_RDEVS_YX;
		ItemUserData.Fun = 0;
		bAddNew = true;

		switch (i)
		{
		case TABLE_TOTALSHOW_DZYJ:
			{
				ItemText = QString::fromLocal8Bit("动作原件");
				ItemUserData.Fun = 4;
			}
			break;
		case TABLE_TOTALSHOW_YX:
			{
				ItemText = QString::fromLocal8Bit("普通遥信");
				ItemUserData.Fun = 7;
			}
			break;
		case TABLE_TOTALSHOW_GZXH:
			{
				ItemText = QString::fromLocal8Bit("故障信号");
				ItemUserData.Fun = 5;
			}
			break;
		case TABLE_TOTALSHOW_GJ:
			{
				ItemText = QString::fromLocal8Bit("告警");
				ItemUserData.Fun = 6;
			}
			break;
		default:
			{
				bAddNew = false;
			}
			break;
		}

		if (bAddNew)
		{
			pNewItem = new QTreeWidgetItem(pParentItem);
			ItemVariData.setValue<TreeItemData>(ItemUserData);
			SetItem(pNewItem, ItemText,ItemVariData);
		}
	}
	return 1;

}


// 修改设备的子Item
int MyTreeWidget::UpdateDeviceChildItem(int ParentId, QTreeWidgetItem *pParentItem)
{
//	if (ParentId > 0)
//	{
		QTreeWidgetItem *pNewItem = NULL;
		QString			 ItemText;
		TreeItemData	 ItemUserData;
		QVariant		 ItemVariData;
		int				 TableNo = 0;
		bool			 bAddNew = false;
		for (TableNo = TABLE_RDEVS_YX; TableNo <= TABLE_RDEVS_OUT/*TABLE_RDEVS_COMMU*/; TableNo++)
		{
			ItemUserData.AppType = SYSTEM_TYPE_NSA_YD;
			if (ParentId>0)
			{
				ItemUserData.parentId = ParentId+TABLE_RDEVS_INFO*PARENT_OFFSET;
				ItemUserData.ParentTableId = TABLE_RDEVS_INFO;
			}

			ItemUserData.ShowTableId = TableNo;
			bAddNew = true;
			switch (TableNo)
			{
			case TABLE_RDEVS_YX:
				{
					ItemText = QString::fromLocal8Bit("遥信");
				}
				break;
			case TABLE_RDEVS_YC:
				{
					ItemText = QString::fromLocal8Bit("遥测");
				}
				break;
			case TABLE_RDEVS_BHYC:
				{
					ItemText = QString::fromLocal8Bit("保护测量");
				}
				break;
			case TABLE_RDEVS_YK:
				{
					ItemText = QString::fromLocal8Bit("遥控");
				}
				break;
			case TABLE_RDEVS_RYB:
				{
					ItemText = QString::fromLocal8Bit("软压板");
				}
				break;
			case TABLE_RDEVS_YM:
				{
					ItemText = QString::fromLocal8Bit("遥脉");
				}
				break;
			case TABLE_RDEVS_SETS:
				{
					ItemText = QString::fromLocal8Bit("定值");
				}
				break;
			case TABLE_RDEVS_PARAM:
				{
					ItemText = QString::fromLocal8Bit("装置参数");
				}
				break;
			case TABLE_RDEVS_JDXSS:
				{
					ItemText = QString::fromLocal8Bit("精度系数");
				}
				break;
			case TABLE_RDEVS_SETAREA:
				{
					ItemText = QString::fromLocal8Bit("定值区");
				}
				break;
			case TABLE_RDEVS_OUT:
				{
					ItemText = QString::fromLocal8Bit("出口状态");
					bAddNew = true;
				}
				break;
// 			case TABLE_RDEVS_COMMU:
// 				{
// 					ItemText = QString::fromLocal8Bit("通信工况");
// 					bAddNew = true;
// 				}
// 				break;
			default:
				{
					bAddNew = false;
				}
				break;
			}

			if (bAddNew)
			{
				pNewItem = new QTreeWidgetItem(pParentItem);
				ItemVariData.setValue<TreeItemData>(ItemUserData);
				SetItem(pNewItem, ItemText,ItemVariData);
				if((pParentItem->text(0)== QString::fromLocal8Bit("总表显示"))&&(pNewItem->text(0)==QString::fromLocal8Bit("遥信")))
				{

					UpdateTotalShowChildItem(0,pNewItem);
				}
			}
		}
		return 1;
//	}

	return 0;
}

int MyTreeWidget::UpdateSysChildItem(QTreeWidgetItem *pParentItem)
{

	if (pParentItem)
	{
		QTreeWidgetItem *pNewItem = NULL;
		QString			 ItemText;
		TreeItemData	 ItemUserData;
		QVariant		 ItemVariData;
		int					TableNo = 0;
		bool			 bAddNew = false;
		for (TableNo = TABLE_RPARAMS_SYS; TableNo <= TABLE_RPARAMS_VERS; TableNo++)
		{
			ItemUserData.AppType = SYSTEM_TYPE_NSA_YD;
			ItemUserData.parentId = 0;
			ItemUserData.ParentTableId = 0;
			ItemUserData.ShowTableId = TableNo;
			bAddNew = false;
			switch (TableNo)
			{
			case TABLE_RPARAMS_SYS:
				{
					ItemText = QString::fromLocal8Bit("系统参数");
					bAddNew = true;
				}
				break;
			case TABLE_RPARAMS_NET:
				{
					ItemText = QString::fromLocal8Bit("网络参数");
					bAddNew = true;
				}
				break;
			case TABLE_RPARAMS_UART:
				{
					ItemText = QString::fromLocal8Bit("串口参数");
					bAddNew = true;
				}
				break;

			case TABLE_RPARAMS_VERS:
				{
					ItemText = QString::fromLocal8Bit("版本信息");
					bAddNew = true;
				}
				break;
			default:
				{
					bAddNew = false;
				}
				break;
			}

			if (bAddNew)
			{
				pNewItem = new QTreeWidgetItem(pParentItem);
				ItemVariData.setValue<TreeItemData>(ItemUserData);
				SetItem(pNewItem, ItemText,ItemVariData);
			}
		}
		return 1;
	}

	return 0;
}

// 修改设备模板的子Item
int MyTreeWidget::UpdateDeviceModelChildItem(int DevModelId, QTreeWidgetItem *pParentItem)
{
	if (pParentItem && DevModelId > 0)
	{
		QTreeWidgetItem *pNewItem = NULL;
		QString			 ItemText;
		TreeItemData	 ItemUserData;
		QVariant		 ItemVariData;
		int					TableNo = 0;
		bool			 bAddNew = false;
		for (TableNo = TABLE_RDEVMODEL_YX; TableNo <= TABLE_RDEVMODEL_SETAREA; TableNo++)
		{
			ItemUserData.AppType = SYSTEM_TYPE_NSA_YD;
			ItemUserData.parentId = DevModelId+TABLE_RDEVMODEL_INFO*PARENT_OFFSET;
			ItemUserData.ParentTableId = TABLE_RDEVMODEL_INFO;
			ItemUserData.ShowTableId = TableNo;
			bAddNew = false;
			switch (TableNo)
			{
			case TABLE_RDEVMODEL_YX:
				{
					ItemText = QString::fromLocal8Bit("遥信模板");
					bAddNew = true;
				}
				break;
			case TABLE_RDEVMODEL_YC:
				{
					ItemText = QString::fromLocal8Bit("遥测模板");
					bAddNew = true;
				}
				break;
			case TABLE_RDEVMODEL_YM:
				{
					ItemText = QString::fromLocal8Bit("遥脉模板");
					bAddNew = true;
				}
				break;
			case TABLE_RDEVMODEL_YK:
				{
					ItemText = QString::fromLocal8Bit("遥控模板");
					bAddNew = true;
				}
				break;
			case TABLE_RDEVMODEL_SET:
				{
					ItemText = QString::fromLocal8Bit("定值模板");
					bAddNew = true;
				}
				break;
			case TABLE_RDEVMODEL_PARA:
				{
					ItemText = QString::fromLocal8Bit("参数模板");
					bAddNew = true;
				}
				break;
			case TABLE_RDEVMODEL_BHYC:
				{
					ItemText = QString::fromLocal8Bit("保护遥测模板");
					bAddNew = true;
				}
				break;
			case TABLE_RDEVMODEL_PREC:
				{
					ItemText = QString::fromLocal8Bit("精度系数模板");
					bAddNew = true;
				}
				break;
			case TABLE_RDEVMODEL_RYB:
				{
					ItemText = QString::fromLocal8Bit("软压板模板");
					bAddNew = true;
				}
				break;
			case TABLE_RDEVMODEL_SETAREA:
				{
					ItemText = QString::fromLocal8Bit("定值区模板");
					bAddNew = true;
				}
				break;
			default:
				{
					bAddNew = false;
				}
				break;
			}
			if (bAddNew)
			{
				pNewItem = new QTreeWidgetItem(pParentItem);
				ItemVariData.setValue<TreeItemData>(ItemUserData);
				SetItem(pNewItem, ItemText,ItemVariData);
			}
		}
		return 1;
	}

	return 0;
}

// 添加Item
QTreeWidgetItem* MyTreeWidget::AddNewItem(int AppType, int TableId, int ChildParentId, char *pRecord, QTreeWidgetItem *pParentItem, int MaxNameLen, int MaxByNameLen)
{
	if (AppType<=0 || !pParentItem)
	{
		return NULL;
	}

	QTreeWidgetItem *pNewItem = NULL;
	QString			ItemText ;
	TreeItemData	ItemUserData;
	QVariant		ItemVariData;

	if (SYSTEM_TYPE_NSA_CD == AppType)
	{
		if (pRecord)
		{
			pNewItem = new QTreeWidgetItem(pParentItem);
			ItemUserData.AppType = AppType;
			ItemUserData.ShowTableId = ((cTable_Info*)pRecord)->TableId;
			ItemText = QString::fromLocal8Bit(((cTable_Info*)pRecord)->TableChnName);
			if (pNewItem)
			{
				ItemVariData.setValue<TreeItemData>(ItemUserData);
				SetItem(pNewItem, ItemText, ItemVariData);
			}
		}

	}else if (SYSTEM_TYPE_NSA_YD == AppType)
	{
		switch (TableId)
		{
		case TABLE_RTASK_INFO:			//任务表
			{
				if (pRecord /*&& ParentId>0*/)
				{
					pNewItem = new QTreeWidgetItem(pParentItem);

					ItemUserData.AppType = AppType;
					ItemUserData.ParentTableId = 0;
					ItemUserData.ShowTableId = TABLE_RTASK_PARA_INFO;
					ItemUserData.parentId = ChildParentId;
					ItemUserData.RecordId = ((rTask_Info*)pRecord)->ID;
					ItemUserData.PtlParamType = g_DbCfgFunObj.GetPtlParamType(((rTask_Info*)pRecord)->m_taskUsage,((rTask_Info*)pRecord)->m_ProtType);
					ItemText = QString::fromLocal8Bit(((rTask_Info*)pRecord)->Name);
					if (pNewItem)
					{
						ItemVariData.setValue<TreeItemData>(ItemUserData);
						SetItem(pNewItem, ItemText, ItemVariData);
					}
				}
			}
			break;
		case TABLE_RDEVS_INFO:			//设备表
			{
				if (pRecord && ChildParentId>0)
				{
					pNewItem = new QTreeWidgetItem(pParentItem);

					ItemUserData.AppType = AppType;
					ItemUserData.ParentTableId = TABLE_RTASK_INFO;
					ItemUserData.ShowTableId = 0;
					ItemUserData.parentId = ChildParentId;
					ItemUserData.RecordId = ((rDevS_Info*)pRecord)->ID;
					ItemText = QString::fromLocal8Bit(((rDevS_Info*)pRecord)->Name);
					if (pNewItem)
					{
						ItemVariData.setValue<TreeItemData>(ItemUserData);
						SetItem(pNewItem, ItemText, ItemVariData);
					}
				}
			}
			break;
		case TABLE_RTRANSMITCONFIG_INFO:			//转发配置表
			{
				if (pRecord && ChildParentId>0)
				{
					pNewItem = new QTreeWidgetItem(pParentItem);

					ItemUserData.AppType = AppType;
					ItemUserData.ParentTableId = 0;
					ItemUserData.ShowTableId = 0;
					ItemUserData.parentId = ChildParentId;
					ItemUserData.RecordId = ((rTransmitConfig_Info*)pRecord)->ID;
					ItemText = QString::fromLocal8Bit(((rTransmitConfig_Info*)pRecord)->Name);
					if (pNewItem)
					{
						ItemVariData.setValue<TreeItemData>(ItemUserData);
						SetItem(pNewItem, ItemText, ItemVariData);
					}
				}
			}
			break;
		case TABLE_RDEVMODEL_INFO:			//设备模板表
			{
				pNewItem = new QTreeWidgetItem(pParentItem);

				ItemUserData.AppType = AppType;
				ItemUserData.ParentTableId = 0;
				ItemUserData.ShowTableId = 0;
				ItemUserData.parentId = ChildParentId;
				ItemUserData.RecordId = ((rDevModel_Info*)pRecord)->ID;
				ItemText = QString::fromLocal8Bit(((rDevModel_Info*)pRecord)->Name);
				if (pNewItem)
				{
					ItemVariData.setValue<TreeItemData>(ItemUserData);
					SetItem(pNewItem, ItemText, ItemVariData);
				}
			}
			break;
		case TABLE_REXPDEFINE_INFO:			//表达式
			{
				pNewItem = new QTreeWidgetItem(pParentItem);

				ItemUserData.AppType = AppType;
				ItemUserData.ParentTableId = 0;
				ItemUserData.ShowTableId = TABLE_REXPVARIABLE_LIST;
				ItemUserData.parentId = ChildParentId;
				ItemUserData.RecordId = ((rExpDefine_Info*)pRecord)->ID;
				ItemText = QString::fromLocal8Bit(((rExpDefine_Info*)pRecord)->Name);
				if (pNewItem)
				{
					ItemVariData.setValue<TreeItemData>(ItemUserData);
					SetItem(pNewItem, ItemText, ItemVariData);
				}
			}
			break;
		case TABLE_RLOGIC_DEVICE:			//61850模板表LD
			{
				if (pRecord /*&& ParentId>0*/)
				{
					pNewItem = new QTreeWidgetItem(pParentItem);

					ItemUserData.AppType = AppType;
					ItemUserData.ParentTableId = 0;
					ItemUserData.ShowTableId = TABLE_RLOGIC_NODE;
					ItemUserData.parentId = ChildParentId;
					//ItemUserData.RecordId = ((rLogic_Device*)pRecord)->ID;
					//ItemUserData.PtlParamType = g_DbCfgFunObj.GetPtlParamType(((rLogic_Device*)pRecord)->,((rLogic_Device*)pRecord)->);
					ItemText = QString::fromLocal8Bit(((rLogic_Device*)pRecord)->Name);
					if (pNewItem)
					{
						ItemVariData.setValue<TreeItemData>(ItemUserData);
						SetItem(pNewItem, ItemText, ItemVariData);
					}
				}
			}
			break;
		case TABLE_RLOGIC_NODE:			//61850模板表LN
			{
				if (pRecord /*&& ParentId>0*/)
				{
					pNewItem = new QTreeWidgetItem(pParentItem);

					ItemUserData.AppType = AppType;
					ItemUserData.ParentTableId = 0;
					ItemUserData.ShowTableId = TABLE_RDATA_OBJECT; //TODO
					ItemUserData.parentId = ChildParentId;
					//ItemUserData.RecordId = ((rLogic_Node*)pRecord)->ID;
					//ItemUserData.PtlParamType = g_DbCfgFunObj.GetPtlParamType(((rLogic_Device*)pRecord)->,((rLogic_Device*)pRecord)->);
					ItemText = QString::fromLocal8Bit("%1:%2").arg(((rLogic_Node*)pRecord)->Name).arg(QString::fromLocal8Bit(((rLogic_Node*)pRecord)->Desc));
					if (pNewItem)
					{
						ItemVariData.setValue<TreeItemData>(ItemUserData);
						SetItem(pNewItem, ItemText, ItemVariData);
					}
				}
			}
			break;
			
// 		case TABLE_RSETPARA_INDEX:					//定值与参数表
// 			{
// 				pNewItem = new QTreeWidgetItem(pParentItem);
// 
// 				ItemUserData.AppType = AppType;
// 				ItemUserData.ShowTableId = TABLE_RSETPARA_INFO;
// 				ItemUserData.parentId = (( rSetPara_Index*)pRecord)->ID;
// 				ItemText = QString::fromLocal8Bit((( rSetPara_Index*)pRecord)->desc);
// 				if (pNewItem)
// 				{
// 					ItemVariData.setValue<TreeItemData>(ItemUserData);
// 					SetItem(pNewItem, ItemText, ItemVariData);
// 				}
// 			}
// 			break;
		default:
			{
				return NULL;
			}
			break;
		}
	}
	return pNewItem;
}


//设置树Item
int MyTreeWidget::SetItem(QTreeWidgetItem *pTreeItem, QString ItemText, QVariant ItemData, int ItemFlag)
{
	if (!pTreeItem)
		return -1;
	QSize size(50, 16);
	pTreeItem->setText(0, ItemText);
	pTreeItem->setData(0, Qt::ToolTipRole, ItemText);
	pTreeItem->setFlags((Qt::ItemFlag)ItemFlag);
	pTreeItem->setData(0, Qt::UserRole, ItemData);
	pTreeItem->setIcon(0, QIcon(":Res/images/open.png"));
	pTreeItem->setSizeHint(0,size);
	return 1;
}

// 修改树当前Item的所有子Item
void MyTreeWidget::OnUpDateTreeItem(quint32 AppType, quint32 TableId, quint32 ParentId, quint32 RecordId, int RowProp)
{
	if (!m_LastSelectItem || AppType<=0 || TableId<=0 || RowProp<=0)
	{
		return ;
	}
	if (RecordId>0)
	{
		InitRecordItem(AppType, TableId, ParentId, m_LastSelectItem, RecordId);
	}else
	{

		DeletechildItem(m_LastSelectItem);
		InitRecordItem(AppType, TableId, ParentId, m_LastSelectItem, 0);
	}
	m_LastSelectItem->setExpanded(true);
}

void MyTreeWidget::OnUpDateTreeItem_byItem(quint32 AppType, quint32 TableId, quint32 ParentId, quint32 RecordId,QTreeWidgetItem *pCurItem, int RowProp)
{
	if (!pCurItem || AppType<=0 || TableId<=0 || RowProp<=0)
	{
		return ;
	}
	
	TreeItemData NewData ;
	NewData = pCurItem->data(0, Qt::UserRole).value<TreeItemData>();
	if (RecordId>0)
	{
		InitRecordItem(AppType, TableId, ParentId, pCurItem, RecordId);
	}else
	{
		DeletechildItem(pCurItem);
		InitRecordItem(AppType, TableId, ParentId, pCurItem, 0);
	}

	pCurItem->setExpanded(true);

}

void MyTreeWidget::OnUpTreeItem_byItem(QTreeWidgetItem *pCurItem)
{
	if (!pCurItem)
	{
		return ;
	}
	
	TreeItemData NewData ;
	NewData = pCurItem->data(0, Qt::UserRole).value<TreeItemData>();
	if (NewData.RecordId>0)
	{
		InitRecordItem(NewData.AppType, NewData.ShowTableId, NewData.parentId, pCurItem, NewData.RecordId);
	}else
	{
		DeletechildItem(pCurItem);
		InitRecordItem(NewData.AppType, NewData.ShowTableId, NewData.parentId,pCurItem, 0);
	}

	pCurItem->setExpanded(true);

}

//添加设备模板
void MyTreeWidget::OnAddTreeDevModelItem(quint32 RecordId)
{	
	QTreeWidgetItem *pItem = NULL;
	QTreeWidgetItem *pItem1 = NULL;
	QList<QTreeWidgetItem *> ItemList;
	ItemList = findItems(QString::fromLocal8Bit("远动表"), Qt::MatchFixedString);
	if (ItemList.count()>0)
	{
		pItem = ItemList[0];
	}

	if (pItem)
	{
		int ChildCount = 0;
		ChildCount = pItem->childCount();
		for (int i=0; i<ChildCount; i++)
		{
			if (0 == pItem->child(i)->text(0).compare(QString::fromLocal8Bit("设备模板"), Qt::CaseInsensitive))
			{
				pItem1 = pItem->child(i);
				break;
			}
		}
		if (pItem1)
		{
			if (RecordId>0)
			{
				InitRecordItem(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_INFO, 0, pItem1, RecordId);
			}else
			{
				DeletechildItem(m_LastSelectItem);
				InitRecordItem(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_INFO, 0, pItem1, 0);
			}
			pItem1->setExpanded(true);
		}	
	}
}

//	删除所有子Item
void MyTreeWidget::DeletechildItem(QTreeWidgetItem *pParentItem)
{
	if (!pParentItem)
	{
		return ;
	}
	QTreeWidgetItem *pItem = NULL;
	int childNum = pParentItem->childCount();
	if (childNum>0)
	{
		while (pParentItem->childCount()>0)
		{
			pItem = NULL;
			pItem = pParentItem->child(0);

			if (pItem)
			{
				pParentItem->removeChild(pItem);
			}
		}
	}
}

void MyTreeWidget::OnClickItem(QTreeWidgetItem *pCurItem)
{
	if (!pCurItem)
	{
		return ;
	}
	if (!pCurItem->isExpanded())
	{
		setItemExpanded(pCurItem, true);
	}
	bool temp = (pCurItem == m_LastSelectItem);
	if (pCurItem != m_LastSelectItem)
	{
		if (m_LastSelectItem)
		{
			m_LastSelectItem->setIcon(0, QIcon(":Res/images/open.png"));
		}
		pCurItem->setIcon(0, QIcon(":Res/images/Selected.png"));
		//ccg如果点击的是逻辑节点配置，则弹出选择文件的窗口
		/*if (pCurItem->text(0)==QString::fromLocal8Bit("逻辑节点配置"))
		{
		    QString fileName = QFileDialog::getOpenFileName(this, tr("open file"), " ",  tr("Allfile(*.*);;icdfile(*.icd)"));
			//读打开的icd文件；
			if (!fileName.isEmpty())
			{
				readIcdFile(fileName);
			}
		}*/
		m_LastSelectItem = pCurItem;
		emit ShowTable_Signal(pCurItem); //ccg:SLOT:OnShowTableData(QTreeWidgetItem*)
	}
}
/*
void MyTreeWidget::readIcdFile(QString fileName)
{
	QFile file(fileName);  
	
	int countNumOfLN = 1;//逻辑节点计数
	int countNumOfDO = 1;
	//logicnodeItem = new _rLogic_Node();
	char	sql[SQL_MAX_LEN];
	char    ValSql[SQL_MAX_LEN];
	memset(sql, 0, SQL_MAX_LEN);
	memset(ValSql, 0, SQL_MAX_LEN);
	char DORelateId[BYNAME_LEN],DOParentId[NAME_SIZE];
	memset(DORelateId,0,BYNAME_LEN);
	memset(DOParentId,0,NAME_SIZE);
	QXmlStreamAttributes attributes;
	QXmlStreamAttributes attributesOfDo;
	QString stringName,stringDesc,stringlnClass;
	QString DONameString,DODescString,DOtypeString;
	if(file.open(QIODevice::ReadOnly | QIODevice::Text))
	{
		//QXmlStreamReader对象设定关联文件  
		//写数据库之前先将数据库清空
		//清空前先提示
		QMessageBox msgBox;
		msgBox.setText(QString::fromLocal8Bit("即将从ICD文件导入数据，数据库中数据将会被删除，时间较长，是否继续？"));
		QPushButton *yesButton = msgBox.addButton(tr("Yes"), QMessageBox::ActionRole);
		QPushButton *noButton = msgBox.addButton(tr("No"), QMessageBox::ActionRole);
		msgBox.exec();

		if (msgBox.clickedButton() == noButton) {
			return;
		}else if (msgBox.clickedButton() ==yesButton)
		{
			msgBox.close();
		}
		//写数据库之前想将数据库清空
		sprintf(sql,"delete from rLogic_Node");
		g_pSqLiteDB.DB_Modify(sql);
		
		sprintf(sql,"delete from rData_Object");
		g_pSqLiteDB.DB_Modify(sql);

		m_xmlRead.setDevice(&file);
		while(!m_xmlRead.atEnd()/ *&&!m_xmlRead.hasError()* /)
		{
			//m_xmlRead.readNext();
			//判断是否是节点的开始
			
			if (m_xmlRead.isStartElement())/ *readNextStartElement()* /	
			{
				
				if(m_xmlRead.name() == "LNodeType")  
				{  
					
					attributes = m_xmlRead.attributes();
					//读逻辑节点属性
					//QString stringName,stringDesc,stringlnClass;
					int Ret = readLNodeTypeAttributes(attributes, &stringName, &stringDesc, &stringlnClass);//希望参数赋值需要用引用（指针）
					//logicnodeItem[countNumOfLN].ID = countNumOfLN;
					//memcpy(&logicnodeItem[countNumOfLN].ID,&countNumOfLN,sizeof(unsigned int));
					//memcpy(logicnodeItem[countNumOfLN].Name,stringName.toLocal8Bit().data(),NAME_SIZE);//char*拷贝给char[]
					//memcpy(logicnodeItem[countNumOfLN].Desc,stringDesc.toLocal8Bit().data(),NAME_SIZE);
					//memcpy(logicnodeItem[countNumOfLN].lnClass,stringlnClass.toLocal8Bit().data(),BYNAME_LEN);
					
					sprintf(ValSql, "%d,0,'%s','%s','%s'",
						countNumOfLN,//id 一定要从1开始
									 //parentid 先强制置为零
						stringName.toLocal8Bit().data(),
						stringDesc.toLocal8Bit().data(),
						stringlnClass.toLocal8Bit().data());//QString 转换为char*
					
					sprintf(sql, "replace into rLogic_Node values( %s)", ValSql);
					int Result = g_pSqLiteDB.DB_Modify(sql);
					countNumOfLN++;
				    //countNumOfDO = 0;//LNodetype下面的DO计数；新的lnodetype从1开始；
										
				} 
				if (m_xmlRead.name() == "DO")
				{
					
					//QString DONameString,DODescString,DOtypeString;
					
					
					//读LNodeType下面的DO
					attributesOfDo = m_xmlRead.attributes();
					int Ret = readDOAttributes(attributesOfDo,&DONameString,&DODescString,&DOtypeString);
					
					
					
					//memcpy(DOParentId,logicnodeItem[countNumOfLN].Name,NAME_SIZE);
					//memcpy(DORelateId,logicnodeItem[countNumOfLN].lnClass,BYNAME_LEN);
					//组织Sql语句
					memset(sql, 0, SQL_MAX_LEN);
					memset(ValSql, 0, SQL_MAX_LEN);
					sprintf(ValSql, "%d,'%d','%s','%s','%s',%d ",
						countNumOfDO,
						countNumOfLN-1+TABLE_RLOGIC_NODE*PARENT_OFFSET, //DOParentId等于逻辑节点的ID+偏移量（父表ID：71）
						DONameString.toLocal8Bit().data(), 
						DODescString.toLocal8Bit().data(),
						DOtypeString.toLocal8Bit().data(),
						0/ *stringlnClass.toLocal8Bit().data()* /);//DORelateId
					
					sprintf(sql, "replace into rData_Object values(%s)", ValSql);
					int Result = g_pSqLiteDB.DB_Modify(sql);
					countNumOfDO++; //如果有DO进入后增1，由1->2;
				}
				

			}
				
			
			m_xmlRead.readNext();
		}
			file.close();
	}

}*/
//xml读逻辑节点属性
/*
int MyTreeWidget::readLNodeTypeAttributes(QXmlStreamAttributes attributes,QString* Name,QString* desc,QString* lnClass)
{
	
	
	if(attributes.hasAttribute("id"))
	{
		//给数据库表TABLE_RLOGIC_NODE插入数值
		 *Name = attributes.value("id").toString();
		 
	}
	if (attributes.hasAttribute("desc"))
	{
		 *desc  = attributes.value("desc").toString();
		
	}
	if (attributes.hasAttribute("lnClass"))
	{
		 *lnClass  = attributes.value("lnClass").toString();
		 
	}
	return 1;

}
//xml读DO属性
int	MyTreeWidget::readDOAttributes(QXmlStreamAttributes attributesOfDo,QString* Name,QString* desc,QString* DOType)
{
	
	if (attributesOfDo.hasAttribute("name"))
	{
		//int len = attributesOfDo.value("name").toString().length();
		*Name  = attributesOfDo.value("name").toString();

	}
	if (attributesOfDo.hasAttribute("desc"))
	{
		*desc = attributesOfDo.value("desc").toString();
	}
	if(attributesOfDo.hasAttribute("type"))
	{
		*DOType = attributesOfDo.value("type").toString();
	}
	return 1;
}

*/

void MyTreeWidget::mousePressEvent( QMouseEvent * event )
{
	if (event)
	{
		if (Qt::RightButton == event->button() &&
			QEvent::MouseButtonPress == event->type())
		{
			QPoint point = event->globalPos();//返回光标在全局的位置
			QTreeWidgetItem * pSeltItem = NULL;
			pSeltItem = itemAt(event->pos()); //光标在树形结构里的位置
			if (pSeltItem )
			{
				if (isItemSelected(pSeltItem))
				{
					emit PopMenu_Signal(point);
				}
			}
			//			return;
		}
	} 
	QTreeWidget::mousePressEvent(event);
}
